package com.producteev4j.services.version0;

import com.producteev4j.exceptions.ProducteevException;
import com.producteev4j.model.json.task.LabelListResponseWrapperImpl;
import com.producteev4j.model.json.task.LabelWrapperImpl;
import com.producteev4j.model.json.task.TaskListWrapperImpl;
import com.producteev4j.model.request.ProducteevGetRequest;
import com.producteev4j.model.request.ProducteevRequest;
import com.producteev4j.model.response.BaseResponse;
import com.producteev4j.model.response.Label;
import com.producteev4j.model.response.LabelWrapper;
import com.producteev4j.model.response.TaskWrapper;
import com.producteev4j.transport.ProducteevTransport;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: jcarrey
 * Date: 2/07/11
 * Time: 12:27
 * To change this template use File | Settings | File Templates.
 */
public class ProducteevLabelServiceV0Impl implements ProducteevLabelService {

    // private static final Logger logger = Logger.getLogger(ProducteevLabelServiceV0Impl.class);

    private static final String LABEL_CONTEXT_PATH = "/labels";

    /*
        labels/view
        labels/show_list
        labels/create
        labels/delete
        labels/tasks
        labels/set_title
     */

    private static final String LABEL_VIEW_SERVICE = "/view.json";
    private static final String LABEL_SHOW_LIST_SERVICE = "/show_list.json";
    private static final String LABEL_CREATE_SERVICE = "/create.json";
    private static final String LABEL_DELETE_SERVICE = "/delete.json";
    private static final String LABEL_TASKS_SERVICE = "/tasks.json";
    private static final String LABEL_SET_TITLE_SERVICE = "/set_title.json";


    private static final String LABEL_VIEW_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + LABEL_CONTEXT_PATH + LABEL_VIEW_SERVICE;
    private static final String LABEL_SHOW_LIST_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + LABEL_CONTEXT_PATH + LABEL_SHOW_LIST_SERVICE;
    private static final String LABEL_CREATE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + LABEL_CONTEXT_PATH + LABEL_CREATE_SERVICE;
    private static final String LABEL_DELETE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + LABEL_CONTEXT_PATH + LABEL_DELETE_SERVICE;
    private static final String LABEL_TASKS_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + LABEL_CONTEXT_PATH + LABEL_TASKS_SERVICE;
    private static final String LABEL_SET_TITLE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + LABEL_CONTEXT_PATH + LABEL_SET_TITLE_SERVICE;


    private static final String ID_LABEL_PARAM = "id_label";
    private static final String ID_DASHBOARD_PARAM = "id_dashboard";
    private static final String PAGE_PARAM = "page";
    private static final String TITLE_PARAM = "title";
    private static final String SINCE_PARAM = "since";

    @Override
    public Label getLabel(ProducteevTransport transport, String userToken, long idLabel) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(LABEL_VIEW_ENDPOINT);
        request.getParams().putValue(ID_LABEL_PARAM, idLabel);
        request.setResponseClass(LabelWrapperImpl.class);
        return (ProducteevValidator.assertNoError((LabelWrapperImpl) transport.process(request, userToken))).getLabel();
    }

    @Override
    public List<? extends LabelWrapper> getLabels(ProducteevTransport transport, String userToken, long idDashboard, Date since, Integer page) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(LABEL_SHOW_LIST_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        if (since != null) {
            request.getParams().putValue(SINCE_PARAM, since);
        }
        if (page != null) {
            request.getParams().putValue(PAGE_PARAM, page);
        }
        request.setResponseClass(LabelListResponseWrapperImpl.class);
        return (ProducteevValidator.assertNoError((LabelListResponseWrapperImpl) transport.process(request, userToken))).getLabels();
    }

    @Override
    public Label createLabel(ProducteevTransport transport, String userToken, long idDashboard, String title) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(LABEL_CREATE_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.getParams().putValue(TITLE_PARAM, title);
        request.setResponseClass(LabelWrapperImpl.class);
        return (ProducteevValidator.assertNoError((LabelWrapperImpl) transport.process(request, userToken))).getLabel();
    }

    @Override
    public boolean deleteLabel(ProducteevTransport transport, String userToken, long idLabel) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(LABEL_DELETE_ENDPOINT);
        request.getParams().putValue(ID_LABEL_PARAM, idLabel);
        request.setResponseClass(BaseResponse.class);
        return !(ProducteevValidator.assertNoError((BaseResponse) transport.process(request, userToken))).isError();
    }

    @Override
    public List<? extends TaskWrapper> getTasksWithLabel(ProducteevTransport transport, String userToken, long idLabel, Integer page) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(LABEL_TASKS_ENDPOINT);
        request.getParams().putValue(ID_LABEL_PARAM, idLabel);
        if (page != null) {
            request.getParams().putValue(PAGE_PARAM, page);
        }
        request.setResponseClass(TaskListWrapperImpl.class);
        return (ProducteevValidator.assertNoError((TaskListWrapperImpl) transport.process(request, userToken))).getTasks();
    }

    @Override
    public Label setLabelTitle(ProducteevTransport transport, String userToken, long idLabel, String title) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(LABEL_SET_TITLE_ENDPOINT);
        request.getParams().putValue(ID_LABEL_PARAM, idLabel);
        request.getParams().putValue(TITLE_PARAM, title);
        request.setResponseClass(LabelWrapperImpl.class);
        return (ProducteevValidator.assertNoError((LabelWrapperImpl) transport.process(request, userToken))).getLabel();

    }
}
